package com.zhyz.bookstore.servlet;

import com.zhyz.bookstore.dao.ManagerDao;
import com.zhyz.bookstore.dao.impl.ManagerDaoImpl;
import com.zhyz.bookstore.entity.Book;
import com.zhyz.bookstore.entity.Cart;
import com.zhyz.bookstore.entity.CartItem;
import com.zhyz.bookstore.entity.Category;
import com.zhyz.bookstore.service.CartService;
import com.zhyz.bookstore.service.ClientService;
import com.zhyz.bookstore.service.ManagerService;
import com.zhyz.bookstore.service.impl.CartServiceImpl;
import com.zhyz.bookstore.service.impl.ClientServiceImpl;
import com.zhyz.bookstore.service.impl.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/Cart/CartServlet")
public class CartServlet extends HttpServlet {
    private ClientService clientservice = new ClientServiceImpl();
    private ManagerService managerService = new ManagerServiceImpl();
    private CartService CartService=new CartServiceImpl();
    private ManagerDao managerdao=new ManagerDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String op = req.getParameter("op");// 得到传过来的请求
        //调用方法
        if (op != null && !op.equals("")) {
            switch (op) {
                //返回列表
                case "category":
                    getCategoryBook(req, resp);
                    break;
                // 添加购物车
                case "addCart":
                    addCart(req, resp);
                    break;
                // 删除购物车中的购物项
                case "delItem":
                    delItem(req, resp);
                    break;
                // 修改购物项数量
                case "changeNum":
                    changeNum(req, resp);
                    break;
                case "buyNow":
                    buyNow(req, resp);
                    break;
            }
        }

    }
    // 通过书籍id找到书籍信息
    private Book findBookById(String bookid) {
//        System.out.println("findBookById:"+bookid);
        Book book = clientservice.findBookById(bookid);
//        System.out.println("findBookById:"+book);
        return book;
    }
    //返回列表
    private void getCategoryBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = clientservice.getCategoryBook(req.getParameter("cid"));// 文学艺术类书籍
        req.setAttribute("books", books);
        List<Category> categoryList= CartService.findAllCategory();
        req.setAttribute("categoryList", categoryList);
        req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
    }
    //增加一条商品到购物车
    private void addCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        System.out.println("增加购物车");
        String bookid = req.getParameter("BookId");
//        System.out.println("addCart"+bookid);
        Book book = findBookById(bookid);
//        System.out.println(book);
        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            session.setAttribute("cart", cart);
        }
        cart.addBook(book);
    }
    //删除单个商品方法
    private void delItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            String bookid = req.getParameter("BookId");
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        cart.getItmes().remove(bookid);
        resp.sendRedirect(req.getContextPath() + "/showCart.jsp");
    }

    //改变数量
    private void changeNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String num = req.getParameter("num");
        String bookid = req.getParameter("BookId");
        // 取出购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        CartItem item = cart.getItmes().get(bookid);
        item.setQuantity(Integer.parseInt(num));
        resp.sendRedirect(req.getContextPath() + "/showCart.jsp");

    }

    //立即购买
    private void buyNow(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String book_id = req.getParameter("book_id");
        Book book = findBookById(book_id);
        HttpSession session = req.getSession();
        Cart cart = new Cart();
        session.setAttribute("buyNowBook", cart);
        cart.addBook(book);
        resp.sendRedirect(req.getContextPath() + "/buyNow.jsp");
    }
}
